package cn.itheima.action;

import java.io.IOException;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;

import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.ActionSupport;

import cn.itheima.domain.Product;
import cn.itheima.domain.Result;
import cn.itheima.service.ProductServiceImpl;

@Namespace("/")
@ParentPackage("bean")
public class ProductAction extends ActionSupport {

	@Action(value = "showProduct", interceptorRefs = { @InterceptorRef("stack") })
	public void showProduct() throws Exception {
		// 处理中文乱码
		ServletActionContext.getResponse().setCharacterEncoding("utf-8");

		ProductServiceImpl serviceImpl = new ProductServiceImpl();
		List<Product> list = serviceImpl.findAll();

		Result<Product> result = new Result<Product>();
		result.setType(1);
		result.setContent(list);
		
		String json = JSONObject.toJSONString(result);
		ServletActionContext.getResponse().getWriter().write(json);
	}

}
